package edu.uc.ui.ctrl25;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.liuvei.common.SysFun;

import edu.uc.bean.Member;
import edu.uc.service.MemberService;
@Repository("LoginAction")
@Scope("prototype")
public class LoginAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	@Autowired
	private MemberService memberService;
	
	private String userName;
	private String userPass;
	private String validateCode;
	private String d;
	

	public String view(){
		String result = "view";
		return result;
	}

	public String loginDeal(){
		String result = "view";
		System.out.println(userName);
		request.put("userName", userName);
		String vMsg = "";
		if(SysFun.isNullOrEmpty(userName)){
			vMsg +="用户名不能为空";
		}
		if(SysFun.isNullOrEmpty(userPass)){
			vMsg +="密码不能为空";
		}
		if(SysFun.isNullOrEmpty(validateCode)){
			vMsg +="验证码不能为空";
			
		}
		else{
			if(!validateCode.equals(session.get("validateCode"))){
				vMsg +="验证码不正确";
			}
		}
		

		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return result;
		}
		
		Member bean = memberService.loadByName(userName);

		if(bean == null){
			request.put("msg", "用户不存在");
			System.out.println(vMsg);
			return result;
		}
		

		if(!bean.getUserPass().equals(userPass)){
			request.put("msg", "密码不正确");
			System.out.println(vMsg);
			return result;
		}

		System.out.println("登录成功");

		session.put("loginuser", bean);

		if(bean.getUserName().equalsIgnoreCase("admin")){
			session.put("isadmin", true);
		}
		result = "main";
		return result;
	}
	
	public String logoutDeal(){
		String result = "view";
		clearLogin();
		result = "login";
		return result;
		
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPass() {
		return userPass;
	}
	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	public String getValidateCode() {
		return validateCode;
	}
	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}
	public String getD() {
		return d;
	}
	public void setD(String d) {
		this.d = d;
	}
	
}
